<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\App\BaseController;
use App\Models\Action;

class ActionController extends BaseController
{
    /**
     * 权限列表
     * @return [type] [description]
     */
    public function actionList(Request $request) 
    {

        $actionModel = new Action();

        $list = $actionModel->ActionList();

        return self::json($list);
    }


    /**
     * 权限添加
     * @return [type] [description]
     */
    public function actionAdd(Request $request) 
    {

        $params = [];
        $name   = $request->input('name');

        if ($name == null) return self::error('权限名称不能为空');

        $request->input('module')       == null || $params['module']       = $request->input('module');
        $request->input('controller')   == null || $params['controller']   = $request->input('controller');
        $request->input('action')       == null || $params['action']       = $request->input('action');
        $request->input('displayorder') == null || $params['displayorder'] = $request->input('displayorder');
        $request->input('status')       == null || $params['status']       = $request->input('status');
        $request->input('pid')          == null || $params['pid']          = $request->input('pid');
        $request->input('level')        == null || $params['level']        = $request->input('level');
        $request->input('formation')    == null || $params['formation']    = $request->input('formation');
        $request->input('url')          == null || $params['url']          = $request->input('url');

        $params['display']      = $request->input('display', 2);
        $params['name'] = $name;

        $actionModel = new Action();
        //对象赋值
        $actionModel->setAttributes($params);

        $result = $actionModel->save();

        return ($result == false) ? self::error('添加失败') : self::success('添加成功');
    }

    /**
     * 权限数据编辑
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function actionEdit(Request $request) 
    {

        $params = [];
        $params['name']         = $request->input('name');
        $params['module']       = $request->input('module');
        $params['controller']   = $request->input('controller');
        $params['action']       = $request->input('action');
        $params['display']      = $request->input('display');
        $params['displayorder'] = $request->input('displayorder');
        $params['status']       = $request->input('status');
        $params['pid']          = $request->input('pid');
        $params['level']        = $request->input('level');
        $params['formation']    = $request->input('formation');
        $params['url']          = $request->input('url');

        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        if (count($params) == 0) return self::success('更新成功');

        $actionModel = Action::find($id);

        if(empty($actionModel)) return self::error('id有误');

        foreach ($params as &$value) {
            if ($value == null) {
                $value = '';
            } 
        }
        
        $actionModel->setAttributes($params);

        $result = $actionModel->save();

        return ($result == false) ? self::error('编辑失败') : self::success('编辑成功');
    }

    /**
     * 权限删除
     * @return [type] [description]
     */
    public function actionDelete(Request $request) 
    {
        
        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $actionModel = Action::find($id);

        if(empty($actionModel)) return self::error('id有误');

        $actionModel->isdelete = Action::IS_DELETE;

        $result = $actionModel->save();

        return ($result == false) ? self::error('删除失败') : self::success('删除成功');
    }


}
